/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haipv.gtkhoa501.screen;

import com.haipv.gtkhoa501.controller.Event;
import com.haipv.gtkhoa501.model.Var;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;

/**
 *
 * @author TRITUEVIET
 */
public class SearchReviewScreen extends BaseScreen implements ActionListener {

    Button search = null;
    TextField txtfield = null;
    Command back = new Command(Var.back);

    public SearchReviewScreen() {
        if (controller.categoryBarReview.getVisibility() == true) {
            controller.categoryBarReview.setVisibility(false);
        }
        if (controller.categoryBarExam.getVisibility() == true) {
            controller.categoryBarExam.setVisibility(false);
        }
    }

    public void showForm() {
        Form form = new Form();
        form.setScrollableY(false);

        Container con = new Container(new BoxLayout(BoxLayout.X_AXIS));

        Label lab = new Label(Var.questionNumber);
        txtfield = new TextField();
        txtfield.setConstraint(TextField.NUMERIC);
        txtfield.setMaxSize(3);
        con.addComponent(lab);
        con.addComponent(txtfield);

        form.addComponent(con);

        search = new Button(Var.goTo);
        search.addActionListener(this);
        form.addComponent(search);

        form.setBackCommand(back);
        form.addCommand(back);
        form.addCommandListener(this);
        form.show();

    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getCommand() == back) {
            Event evt = new Event();
            evt.setKey("INDEX", Var.indexReview + "");
            controller.handleEvent(Event.REVIEW_SEEK, evt);
        } else if (ae.getSource() == search) {
            String txt = txtfield.getText().trim();
            boolean kt = true;
            for (int i = 0; i < txt.length(); i++) {
                if (txt.charAt(i) != '0' && txt.charAt(i) != '1' && txt.charAt(i) != '2' && txt.charAt(i) != '3'
                        && txt.charAt(i) != '4' && txt.charAt(i) != '5' && txt.charAt(i) != '6' && txt.charAt(i) != '7'
                        && txt.charAt(i) != '8' && txt.charAt(i) != '9') {
                    controller.showMessenger("thông báo", "Bạn cần nhập đúng số. Số câu nằm trong đoạn từ 1 tới 450.");
                    txtfield.clear();
                    kt = false;
                    return;
                }
            }
            if (kt == true) {
                if (txt.equals("") || Integer.parseInt(txt) > 450 || Integer.parseInt(txt) < 1) {
                    controller.showMessenger("thông báo", "Bạn cần nhập đúng số. Số câu nằm trong đoạn từ 1 tới 450.");
                    return;
                } else {
                    Var.indexReview = Integer.parseInt(txt);
                    Event evt = new Event();
                    evt.setKey("INDEX", Var.indexReview + "");
                    controller.handleEvent(Event.REVIEW_SEEK, evt);
                }
            }
        }
    }
}
